package com.zr.service.order.impl;

import com.zr.dao.order.OrderFeeDao;
import com.zr.service.order.OrderFeeService;
import com.zr.vo.order.OrderFee;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单费用处理
 * @author huangguilong
 */
@Service
public class OrderFeeServiceImpl implements OrderFeeService {

    @Resource
    private OrderFeeDao orderFeeDao;

    // 注入默认订单编号
    @Value("${orderId.default}")
    private String orderIdDefault;

    /**
     * 根据订单号获取费用信息
     * @param orderId
     * @return Map<String, Object>
     * @author huangguilong
     */
    @Override
    public Map<String, Object> getFeeByOrderId(String orderId) {
        List<OrderFee> orderFeeList = orderFeeDao.selectFeeListByOrderId(orderId);
        Float orderFeeTotal = 0.0F;
        // 计算订单总费用
        for (OrderFee orderFee : orderFeeList) {
            orderFeeTotal += orderFee.getFeeIncludeTax();
        }
        Map<String, Object> map = new HashMap<>();
        map.put("orderFeeTotal", orderFeeTotal);
        map.put("orderFeeList", orderFeeList);
        return map;
    }

    /**
     * 新增订单费用
     * @param orderFee
     * @return int
     * @author huangguilong
     */
    @Override
    public int add(OrderFee orderFee) {
        return orderFeeDao.insertSelective(orderFee);
    }

    /**
     * 移除订单费用
     * @param orderFee
     * @return int
     * @author huangguilong
     */
    @Override
    public int remove(OrderFee orderFee) {
        return orderFeeDao.deleteByPrimaryKey(orderFee.getId());
    }

    /**
     * 删除所有未生成订单的费用
     * @return int
     * @author huangguilong
     */
    @Override
    public int deleteAllByOrderId() {
        // orderIdDefault 注入为 WT************
        return orderFeeDao.deleteAllByOrderId(orderIdDefault);
    }
}
